// pages/index/index.js
const mockData = [
  {
    id: 1,
    user: {
      name: "小明",
      avatar: "/images/avatar1.jpg"
    },
    content: "测试朋友圈样式",
    images: ["/images/post1.jpg"],
    time: "1小时前",
    isLiked: false,
    likes: 5,
    comments: [
      {
        user: { name: "小红" },
        content: "这个实现很赞！",
        time: "30分钟前"
      }
    ]
  },
  // 更多模拟数据...
]

Page({
  data: {
    postList: [],
    currentCommentIndex: -1,
    commentContent: "",
    page: 0,
    isLoading: false,
    hasMore: true
  },

  onLoad() {
    this.loadData(true)
  },

  // 加载数据
  loadData(init = false) {
    if (this.data.isLoading || !this.data.hasMore) return

    this.setData({ isLoading: true })
    
    // 模拟网络请求
    setTimeout(() => {
      const newData = mockData.slice(this.data.page * 2, (this.data.page + 1) * 2)
      this.setData({
        postList: init ? newData : [...this.data.postList, ...newData],
        page: this.data.page + 1,
        isLoading: false,
        hasMore: this.data.page < 2 // 模拟只有3页数据
      })
    }, 1000)
  },

  // 上拉加载
  loadMore() {
    this.loadData()
  },

  // 点赞/取消点赞
  toggleLike(e) {
    const index = e.currentTarget.dataset.index
    const postList = this.data.postList
    const post = postList[index]
    
    post.isLiked = !post.isLiked
    post.likes += post.isLiked ? 1 : -1
    
    this.setData({ postList })
  },

  // 显示/隐藏评论框
  toggleComment(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      currentCommentIndex: this.data.currentCommentIndex === index ? -1 : index,
      commentContent: ""
    })
  },

  // 输入评论
  onCommentInput(e) {
    this.setData({ commentContent: e.detail.value })
  },

  // 提交评论
  submitComment(e) {
    const index = e.currentTarget.dataset.index
    const content = this.data.commentContent.trim()
    if (!content) return

    const postList = this.data.postList
    postList[index].comments.push({
      user: { name: "当前用户" },
      content: content,
      time: "刚刚"
    })

    this.setData({
      postList,
      commentContent: "",
      currentCommentIndex: -1
    })
  },

  // 图片预览
  previewImage(e) {
    const urls = e.currentTarget.dataset.urls
    const index = e.currentTarget.dataset.index
    wx.previewImage({
      current: urls[index],
      urls: urls
    })
  },
  // pages/index/index.js 添加方法
navigateToPublish() {
  wx.navigateTo({ url: '/pages/index/publish' })
}
})